<div v-cloak id="app" class="main-content">
    <div class="form-body">
        <one-form :config="config" v-model="fdata" ref="ruleForm">
            <div slot="menuLeft">
                <el-button
                    class="button-item"
                    :loading="btnLoading"
                    type="primary"
                    @click="store('ruleForm')"
                    size="small"
                >
                    保存
                </el-button>
                <el-button size="small" @click="backpage()">返回</el-button>
            </div>
            <template
                slot="form-item-end"
                slot-scope="scope"
                v-if="scope.row.tip"
            >
                <span style="left: 400px; position: absolute; font-size: 12px"
                    >{{scope.row.tip}}</span
                >
            </template>
        </one-form>
        <div class="form-remark">
            <div class="el-divider el-divider--horizontal"><!----></div>
            <div class="content">
                <p>
                    此处为解析地址存储表，目的为减少解析接口的请求次数，进行解析时优先查询表中的记录，若真实播放地址失效或暂未找到记录，将请求解析接口获取，同时自动记录入库
                </p>
                <p>
                    你也可以在解析接口出现暂时无法解析等突发情况时，在此手动添加替换地址，作为临时应急
                </p>
            </div>
        </div>
    </div>
</div>
{include file='common@components/one-form'} {include
file='common@components/one-upload'}
<script>
    const _formData = JSON.parse('{:json_encode((array)$formData, 1)}');
    const app = new Vue({
        el: '#app',
        data() {
            return {
                uploading: false,
                loading: false,
                btnLoading: false,
                config: {
                    formdesc: [
                        {
                            label: '资源索引 :',
                            prop: 'unique',
                            type: 'input',
                            rules: [
                                { required: true, message: '资源索引不能为空' },
                            ],
                            bind: {
                                readonly: 'readonly',
                            },
                        },
                        {
                            label: '视频id :',
                            prop: 'source_id',
                            type: 'input',
                            rules: [
                                { required: true, message: '视频id不能为空' },
                            ],
                            bind: {
                                readonly: 'readonly',
                            },
                        },
                        {
                            label: '视频标题 :',
                            prop: 'source_title',
                            type: 'input',
                            rules: [
                                { required: true, message: '视频标题不能为空' },
                            ],
                            bind: {
                                readonly: 'readonly',
                            },
                        },
                        {
                            label: '源地址 :',
                            prop: 'source_url',
                            type: 'input',
                            rules: [
                                { required: true, message: '源地址不能为空' },
                            ],
                            bind: {
                                placeholder: '请输入源地址',
                            },
                            content: '官方播放地址',
                        },
                        {
                            label: '替换地址 :',
                            prop: 'format_url',
                            type: 'input',
                            rules: [
                                { required: true, message: '替换地址不能为空' },
                            ],
                            bind: {
                                placeholder: '请输入替换地址',
                            },
                            content: '替换地址',
                        },
                    ],
                    labelWidth: '200px',
                    rowSize: 1, //一行可以展示几列表单，默认为3列
                },
                fdata: {
                    id: 0,
                    source_url: '',
                    format_url: '',
                },
            };
        },
        created() {},
        mounted() {
            let _this = this;
            if (Object.keys(_formData).length > 0) {
                for (let key in _this.fdata) {
                    _this.fdata[key] = _formData[key];
                }
            }
        },
        methods: {
            //表单验证
            getFormPromise(form) {
                return new Promise((resolve) => {
                    form.validate((res) => {
                        resolve(res);
                    });
                });
            },
            store(formName) {
                let _this = this;
                // 获取到组件中的form
                const configForm = this.$refs.ruleForm.$refs.ruleForm;
                // 使用Promise.all去校验结果,可加入多个表单
                Promise.all([configForm].map(this.getFormPromise)).then(
                    (res) => {
                        const validateResult = res.every((item) => !!item);
                        if (validateResult) {
                            _this.loading = true;
                            request({
                                params: {
                                    s: 'videos/analysis/edit',
                                },
                                method: 'post',
                                data: _this.fdata,
                            }).then((e) => {
                                _this.loading = false;
                                if (e.data.code == 0) {
                                    _this.backpage();
                                } else {
                                    _this.$message.error(e.data.msg);
                                }
                            });
                        } else {
                            console.log('表单未校验通过');
                        }
                    }
                );
            },
            backpage() {
                navigateTo({
                    s: 'videos/analysis/index',
                });
            },
            //图片上传完成
            uploadSuccess(file, prop) {
                file = file.response.data.data.file;
                this.fdata[prop] = file;
            },
        },
    });
</script>
